/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgModuloPrincipal.pkgFachadaPrincipal;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import pkgMetodoPyramid.pkgPresentacion.PllaPyramid;
import pkgMetodoPyramid.pkgSoporte.Constants;
import pkgModuloPrincipal.pkgFachadaPrincipal.pkgOpcionSistema.OpcionSistema;
import pkgUtil.Message;
import pkgUtil.PASConstants;
import pkgUtil.UtilLayoutPantalla;

/**
 *
 * @author Administrador
 */
public class PllaPrincipalSistema extends javax.swing.JFrame {

    private GestorPrincipalSistema gestorPpalSistema;
    private static javax.swing.JDesktopPane ppalDesktopPane;

    /**
     * Creates new form PllaPrincipalSistema
     */
    public PllaPrincipalSistema() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dPPrincipal = new javax.swing.JDesktopPane();
        jSPOpcionesSistema = new javax.swing.JScrollPane();
        jTOpcionesSistema = new javax.swing.JTree();
        panelTituloPAS = new javax.swing.JPanel();
        lblTituloPAS = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        mnuBarOpcionesPAS = new javax.swing.JMenuBar();
        mnuArchivo = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PAS");
        setName("pllaPrincipalSistema");

        dPPrincipal.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        dPPrincipal.setAutoscrolls(true);

        jTOpcionesSistema.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTOpcionesSistemaMousePressed(evt);
            }
        });
        jSPOpcionesSistema.setViewportView(jTOpcionesSistema);

        panelTituloPAS.setBackground(new java.awt.Color(153, 153, 255));
        panelTituloPAS.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblTituloPAS.setFont(new java.awt.Font("Californian FB", 3, 48)); // NOI18N
        lblTituloPAS.setForeground(new java.awt.Color(255, 255, 255));
        lblTituloPAS.setText("PAS");

        javax.swing.GroupLayout panelTituloPASLayout = new javax.swing.GroupLayout(panelTituloPAS);
        panelTituloPAS.setLayout(panelTituloPASLayout);
        panelTituloPASLayout.setHorizontalGroup(
            panelTituloPASLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloPASLayout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(lblTituloPAS)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelTituloPASLayout.setVerticalGroup(
            panelTituloPASLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTituloPASLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblTituloPAS)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        mnuArchivo.setText("Archivo");
        mnuBarOpcionesPAS.add(mnuArchivo);

        setJMenuBar(mnuBarOpcionesPAS);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSPOpcionesSistema, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
                        .addComponent(panelTituloPAS, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dPPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 969, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(panelTituloPAS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSPOpcionesSistema, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(69, 69, 69)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(dPPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 723, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 76, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTOpcionesSistemaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTOpcionesSistemaMousePressed
        if (evt.getClickCount() == 2) {
            opcionSeleccionOpcion();
        }
    }//GEN-LAST:event_jTOpcionesSistemaMousePressed

    private void opcionSeleccionOpcion() {

        DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode) jTOpcionesSistema.getSelectionPath().getLastPathComponent();
        if (!(nodeSelected.getUserObject() instanceof OpcionSistema)) {
            return;
        }
        try {
            OpcionSistema opcSelected = (OpcionSistema) nodeSelected.getUserObject();
            gestorPpalSistema.ejecutarOpcionSistema(opcSelected);
        } catch (Exception ex) {
            Message.mensaje_Estandar("Ocurrió un error al ejecutar la opción seleccionada. Reinicie el sistema y vuelta a intentar.", "Mensaje", Message.TIPO_ERROR);
        }

    }

    public void abrirPantalla(GestorPrincipalSistema gestor) {
        try {
            gestorPpalSistema = gestor;
            cargarOpcionesSistema();
            ppalDesktopPane = dPPrincipal;
            setLayoutJTree();
            UtilLayoutPantalla.centrarEnPantallaJFrame(this);
            setVisible(true);
        } catch (Exception ex) {
            Message.mensaje_Estandar("Error al inicializar pantalla principal: " + ex.getMessage(), "Mensaje", Message.TIPO_ERROR);
            this.dispose();
        }
    }

    private void cargarOpcionesSistema() {
        //Se crea el padre ppal
        DefaultMutableTreeNode ppalTreeNode = new DefaultMutableTreeNode("Métodos");
        DefaultTreeModel dTModel = new DefaultTreeModel(ppalTreeNode);

        Iterator<OpcionSistema> itOpciones = gestorPpalSistema.getOpcionesSistemas();
        while (itOpciones.hasNext()) {
            OpcionSistema opcSistema = itOpciones.next();
            DefaultMutableTreeNode hijoTreeNode = new DefaultMutableTreeNode(opcSistema);
            dTModel.insertNodeInto(hijoTreeNode, ppalTreeNode, opcSistema.getNumeroOrdenOpcion());
        }
        jTOpcionesSistema.setModel(dTModel);
    }

    private void setLayoutJTree() throws IOException {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        
        InputStream isClosed = getClass().getResourceAsStream(PASConstants.PATH_IMG_CLOSED_TREE);
        BufferedImage buffImageClosed = ImageIO.read(isClosed);
        
        InputStream isOpen = getClass().getResourceAsStream(PASConstants.PATH_IMG_OPEN_TREE);
        BufferedImage buffImageOpen = ImageIO.read(isOpen);
        
        renderer.setOpenIcon(new ImageIcon(buffImageOpen));
        renderer.setClosedIcon(new ImageIcon(buffImageClosed));
        renderer.setLeafIcon(new ImageIcon(buffImageClosed));
        jTOpcionesSistema.setCellRenderer(renderer);
    }

    /**
     * @return the ppalDesktopPane
     */
    public static javax.swing.JDesktopPane getPpalDesktopPane() {
        return ppalDesktopPane;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane dPPrincipal;
    private javax.swing.JScrollPane jSPOpcionesSistema;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTree jTOpcionesSistema;
    private javax.swing.JLabel lblTituloPAS;
    private javax.swing.JMenu mnuArchivo;
    private javax.swing.JMenuBar mnuBarOpcionesPAS;
    private javax.swing.JPanel panelTituloPAS;
    // End of variables declaration//GEN-END:variables
}
